using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Web;
using System.IO;

namespace Pixysoft.Framework.Security
{
    class SSoCommandController
    {
        public const string default_attribute_token = "token";
        public const string default_attribute_command = "command";
        private const string default_element_request = "request";
        private const string default_element_response = "response";
        private const string default_attribute_status = "status";

        private const int default_attribute_status_succeed = 1;
        private const int default_attribute_status_fail = 0;

        private static volatile SSoCommandController instance;

        private static object syncRoot = new Object();

        public static SSoCommandController Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new SSoCommandController();
                    }
                }

                return instance;

            }
        }


        public T GetSSoRequest<T>(string base64content)
        {
            string encryptcontent = Pixysoft.Security.Base64.Decript(base64content);
            string compresscontent = Pixysoft.Security.DES.Decrypt(encryptcontent, StringHelper.global_framework_id);
            string content = Pixysoft.IO.Compressor.Instance.DeCompress(compresscontent);
            return XmlManager.Deserialize<T>(content);
        }

        public string GetSSoResponse<T>(T response)
        {
            string content = Pixysoft.IO.XmlExporter.Instance.XmlSerialize(response);
            return GetSSoResponse(content);
        }

        public string GetSSoResponse(string response)
        {
            string content = response;
            string compresscontent = Pixysoft.IO.Compressor.Instance.Compress(content);
            string encryptcontent = Pixysoft.Security.DES.Encrypt(compresscontent, StringHelper.global_framework_id);
            string base64content = Pixysoft.Security.Base64.Encript(encryptcontent);
            return base64content;
        }


        public string CreateSimpleRequest(string token, string command, string message)
        {
            return CreateRequest(token, command, message).Serialize();
        }

        public string CreateSimpleResponse(string token, string command, bool succeed, string message)
        {
            IXmlNode node = CreateResponse(token, command, succeed);
            node.Text = message;
            return node.Serialize();
        }


        public IXmlNode CreateRequest(string token, string command, string text)
        {
            IXmlNode node = XmlManager.CreateNode(default_element_request);
            node.Properties.Add(default_attribute_token, token);
            node.Properties.Add(default_attribute_command, command);
            node.Text = text;
            return node;
        }

        public IXmlNode CreateResponse(string token, string command, bool succeed)
        {
            IXmlNode node = XmlManager.CreateNode(default_element_response);
            node.Properties.Add(default_attribute_token, token);
            node.Properties.Add(default_attribute_command, command);
            node.Properties.Add(default_attribute_status, succeed ? "1" : "0");
            return node;
        }

        public IXmlNode CreateResponse(string token, string command, bool succeed, string text)
        {
            IXmlNode node = XmlManager.CreateNode(default_element_response);
            node.Properties.Add(default_attribute_token, token);
            node.Properties.Add(default_attribute_command, command);
            node.Properties.Add(default_attribute_status, succeed ? "1" : "0");
            node.Text = text;
            return node;
        }

        public bool IsSucceed(string response)
        {
            if (string.IsNullOrEmpty(response))
                return false;

            IXmlNode node = Deserialize(response);

            if (node == null)
                return false;

            return default_attribute_status_succeed.ToString().Equals(
                node.Properties[default_attribute_status], StringComparison.OrdinalIgnoreCase);
        }

        public IXmlNode Deserialize(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;

            return XmlManager.Deserialize(value);
        }

        public SSoCommand GetCommandType(string value)
        {
            if (string.IsNullOrEmpty(value))
                return SSoCommand.UNKNOWN;

            return Pixysoft.EnumCoder.ToEnum<SSoCommand>(value);
        }
    }
}
